export class JsUtil {
    public static formatSeconds(value: any) {
        let result = parseInt(value)
        let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
        let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
        let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

        let res = '';
        if (h !== '00') res += `${h}时`;
        if (m !== '00') res += `${m}分`;
        res += `${s}秒`;
        return res;
    }

    public static localStorageAddItem(key: string, item: any) {
        key = "array-" + key;
        let arrStr = localStorage.getItem(key);
        if (arrStr) {
            const arr = JSON.parse(arrStr);
            arr.push(item);
            arrStr = JSON.stringify(arr);
        } else {
            arrStr = JSON.stringify([item]);
        }
        localStorage.setItem(key, arrStr);
    }

    public static localStorageGetItem(key: string) {
        key = "array-" + key;
        let arrStr = localStorage.getItem(key);
        if (arrStr) {
            return JSON.parse(arrStr);
        } else {
            return [];
        }
    }
    public static localStorageremoveItem(key: string,filed:string,value:any) {
        key = "array-" + key;
        let arrStr = localStorage.getItem(key);
        if (arrStr) {
            const arr =  JSON.parse(arrStr);
            const result = arr.filter((item:any)=>{
                return item[filed] !== value;
            })
            localStorage.setItem(key, JSON.stringify(result));
        }
    }
    public static localStorageremoveItemAll(key: string) {
        key = "array-" + key;
        localStorage.removeItem(key);
    }



    public static arrayUnique(arr:Array<any>,field:string) {
        if(!arr){
            return [];
        }
        var result = [], hash:any = {};
        for(let item of arr){
            const v = item[field];
            if (!hash[v]) {
                result.push(item);
                hash[v] = true;
            }
        }
        return result;
    }
    public static rad(d: number) {
        return d * Math.PI / 180.0;
    }

    public static getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
        // 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
        var radLat1 = JsUtil.rad(lat1);
        var radLat2 = JsUtil.rad(lat2);
        var a = radLat1 - radLat2;
        var b = JsUtil.rad(lng1) - JsUtil.rad(lng2);
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137; // EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000; //输出为公里
        var distance: any = s;
        var distance_str = "";
        if (parseInt(distance) >= 1) {
            distance_str = distance.toFixed(1) + "km";
        } else {
            distance_str = distance * 1000 + "m";
        }
        return {s,distance_str};
    }
}
